package com.codeyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeyang.domain.Prod;
import com.codeyang.domain.Sku;
import com.codeyang.service.ProdCommService;
import com.codeyang.service.ProdService;
import com.codeyang.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/5/25 14:04
 */
@Api(tags = "商品接口管理")
@RestController
@RequestMapping("prod/prod")
public class ProdController {


    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;


    @Autowired
    private ProdCommService prodCommService;

    @ApiOperation("分页查询商品")
    @GetMapping("page")
    @PreAuthorize("hasAnyAuthority('prod:prod:page')")
    public ResponseEntity<Page<Prod>> page(Page<Prod> page, Prod prod) {
        Page<Prod> prodPage = prodService.page(page,
                new LambdaQueryWrapper<Prod>()
                        .eq(!ObjectUtils.isEmpty(prod.getStatus()), Prod::getStatus, prod.getStatus())
                        .like(StringUtils.hasText(prod.getProdName()), Prod::getProdName, prod.getProdName())
                        .orderByDesc(Prod::getUpdateTime)
        );
        return ResponseEntity.ok(prodPage);
    }


    @ApiOperation("新增商品")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public ResponseEntity<Void> addProd(@RequestBody Prod prod) {
        //新增之前有很多数据加载的前置操作 prod/spec/lis ......
        // 这里对商品的新增有很多其他数据的操作,我们需要重写方法
        prodService.save(prod);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除一个商品")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        prodService.removeById(id);
        return ResponseEntity.ok().build();
    }


    @DeleteMapping
    @ApiOperation("删除多个商品")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public ResponseEntity<Void> delete(@RequestBody List<Long> list) {
        prodService.removeByIds(list);
        return ResponseEntity.ok().build();
    }

    // ----------------------- 提供远程调用的接口-------------------

    /**
     * 远程调用根据商品id查询商品信息
     * 这个是需要被其他服务调用的
     *
     * @param prodId
     * @return
     */
    @GetMapping("getProdInfoById")
    @ApiOperation("提供远程调用根据商品id查询商品信息")
    public Prod getProdInfoById(Long prodId) {
        return prodService.getById(prodId);
    }

    /**
     * 远程调用获取sku集合
     *
     * @param skuIds
     * @return
     */
    @PostMapping("getSkusBySkuIds")
    @ApiOperation("根据skuIds远程调用获取sku集合")
    public List<Sku> getSkusBySkuIds(@RequestBody List<Long> skuIds) {
        return skuService.listByIds(skuIds);
    }


    //-----前台调用的-----

    /**
     * 根据商品id查询商品信息和sku信息 前台服务需要使用的
     *
     * @param prodId
     * @return
     */
    @GetMapping("prod/prodInfo")
    @ApiOperation("根据商品id查询商品信息和sku信息")
    public Prod getProdInfoAndSkuById(@RequestParam Long prodId) {

        return prodService.getProdInfoAndSkuById(prodId);
    }

    /**
     * 远程调用修改库存
     * data.put("prod", prodIdsMap);
     * data.put("sku", skuIdsMap);
     *
     * @param data
     */
    @PostMapping("changeStock")
    public void changeStock(@RequestBody Map<String, Map<Long, Integer>> data) {
        prodService.changStock(data);
    }


}
